package com.ecloud.support.generator.controller;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ecloud.common.base.result.Result;
import com.ecloud.common.base.valid.ValidGroup.Insert;
import com.ecloud.common.base.valid.ValidGroup.QueryList;
import com.ecloud.common.base.valid.ValidGroup.Update;
import com.ecloud.common.utils.check.StringUtils;
import com.ecloud.support.generator.custom.DatabaseEnum;
import com.ecloud.support.generator.model.dto.DbConfigDto;
import com.ecloud.support.generator.service.iface.IDbConfigService;

@RestController
@RequestMapping(value = { "/db" })
public class DbConfigController {
    @Resource
    private IDbConfigService service;

    /**
     * 测试连接
     *
     * @author LiuGangQiang Create in 2021/04/01
     * @param dto 入参
     * @return {@link Result}
     */
    @PostMapping("/config/test")
    public Result<?> test(@RequestBody @Validated({ Insert.class }) DbConfigDto dto) {
        /* 设置驱动路径 */
        dto.setDriver(DatabaseEnum.fromType(dto.getDbType()).getDriver());
        return service.test(dto);
    }

    /**
     * 新增数据
     *
     * @author LiuGangQiang Create in 2021/04/01
     * @param dto 入参
     * @return {@link Result}
     */
    @PostMapping("/config")
    public Result<?> insert(@RequestBody @Validated({ Insert.class }) DbConfigDto dto) {
        return service.insert(dto);
    }

    /**
     * 删除数据
     *
     * @author LiuGangQiang Create in 2021/04/01
     * @param id ID
     * @return {@link Result}
     */
    @DeleteMapping("/config/{id}")
    public Result<?> delete(@PathVariable("id") String id) {
        DbConfigDto dto = new DbConfigDto();
        dto.setId(id);
        return service.delete(dto);
    }

    /**
     * 修改数据
     *
     * @author LiuGangQiang Create in 2021/04/01
     * @param id  ID
     * @param dto 入参
     * @return {@link Result}
     */
    @PutMapping("/config/{id}")
    public Result<?> update(@PathVariable("id") String id, @RequestBody @Validated({ Update.class }) DbConfigDto dto) {
        dto.setId(id);
        /* 设置驱动路径 */
        if (StringUtils.isNotBlank(dto.getDbType())) {
            dto.setDriver(DatabaseEnum.fromType(dto.getDbType()).getDriver());
        }
        return service.update(dto);
    }

    /**
     * 查询列表
     *
     * @author LiuGangQiang Create in 2021/04/01
     * @param dto 入参
     * @return {@link Result}
     */
    @GetMapping("/configs")
    public Result<?> queryList(@Validated({ QueryList.class }) DbConfigDto dto) {
        return service.queryList(dto);
    }
}
